package com.xinya.taobaosale.ui.activity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.xinya.taobaosale.R;
import com.xinya.taobaosale.ui.adapter.MainContainerAdapter;
import com.xinya.taobaosale.ui.fragment.HomeFragment;
import com.xinya.taobaosale.ui.fragment.SaleFragment;
import com.xinya.taobaosale.ui.fragment.SearchFragment;
import com.xinya.taobaosale.ui.fragment.SelectFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends BaseActivity {

    private static final String TAG = "MainActivity";

    @BindView(R.id.navBottomView)
    BottomNavigationView navBottomView;
    @BindView(R.id.nav_main_container)
    ViewPager2 navMainContainer;

    @Override
    protected int getContentViewByViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initEvent() {
        navBottomView.setOnNavigationItemSelectedListener(menuItem -> {
            switch (menuItem.getItemId()) {
                case R.id.homeFragment:
                    navMainContainer.setCurrentItem(0, false);
                    break;
                case R.id.selectFragment:
                    navMainContainer.setCurrentItem(1, false);
                    break;
                case R.id.saleFragment:
                    navMainContainer.setCurrentItem(2, false);
                    break;
                case R.id.searchFragment:
                    navMainContainer.setCurrentItem(3, false);
                    break;
            }
            return true;
        });
    }

    @Override
    protected void initView() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new SelectFragment());
        fragmentList.add(new SaleFragment());
        fragmentList.add(new SearchFragment());
        MainContainerAdapter mainContainerAdapter =
                new MainContainerAdapter(fragmentList, getSupportFragmentManager(), getLifecycle());
        // 禁止用户滑动
        navMainContainer.setUserInputEnabled(false);
        navMainContainer.setOffscreenPageLimit(fragmentList.size());
        navMainContainer.setAdapter(mainContainerAdapter);
    }

}
